package com.javaweb.util.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

//JDK动态代理（基于接口）
public class JdkDynamicProxy implements InvocationHandler {
	
    private Object object;
    
    public JdkDynamicProxy(Object object) {
        this.object = object;
    }
    
    public Object getProxy() {
        ClassLoader classLoader = object.getClass().getClassLoader();
        Class<?>[] interfaces = object.getClass().getInterfaces();
        Object newObject = Proxy.newProxyInstance(classLoader,interfaces,this);
        return newObject;
    }
    
    @Override
	public Object invoke(Object proxy,Method method,Object[] args) throws Throwable {
		//System.out.println(method.getName());
		return method.invoke(object);
	}
    
    /**
    public static void main(String[] args) {
    	var user = new User();
    	JdkDynamicProxy jdkDynamicProxy = new JdkDynamicProxy(user);
        UserDao proxy = (UserDao)jdkDynamicProxy.getProxy();
        proxy.insert();
	}
	*/

}
